﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CommDll;

namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmMedicineCodeCheck
    /// Version:1.0
    /// Date:2016-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现药品对码
    /// </remarks>
    public partial class frmGoodsCodeCheck : DevComponents.DotNetBar.OfficeForm
    {
        public frmGoodsCodeCheck()
        {
            InitializeComponent();
        }

        #region  1.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 库房数据
            FunctionComm._setInfoToCbb_Dt(cbbWarehouse, AppInfo.m_dtWarehouse);
            // 控件赋初始值
            txtCode0.Text = ModelGoodsCodeCheck.strCode0;
            txtUnit0.Text = ModelGoodsCodeCheck.strUnit0;
            txtUnitMinForm0.Text = string.IsNullOrEmpty(ModelGoodsCodeCheck.strUnitMinForm0) ? ModelGoodsCodeCheck.strUnit1 : ModelGoodsCodeCheck.strUnitMinForm0;  // 如为空,默认为HIS单位            
            nudWarehouseRatio0.Value = (decimal)ModelGoodsCodeCheck.dWarehouseRatio0;
            txtName0.Text = ModelGoodsCodeCheck.strName0;
            txtForm0.Text = ModelGoodsCodeCheck.strForm0;
            txtApproveCode0.Text = ModelGoodsCodeCheck.strRegistrationCode0;
            txtSpec0.Text = ModelGoodsCodeCheck.strSpec0;
            txtMaker0.Text = ModelGoodsCodeCheck.strManufacturer0;
            txtKind0.Text = ModelGoodsCodeCheck.strKind0;
            txtPrice0.Text = string.Format("{0:0.00}", string.IsNullOrEmpty(ModelGoodsCodeCheck.strPrice0) ? ModelGoodsCodeCheck.dPrice1 : FunctionComm._strToFloat(ModelGoodsCodeCheck.strPrice0));            
            cbbWarehouse.Text = ModelGoodsCodeCheck.strWarehouseName0 + '|' + ModelGoodsCodeCheck.strWarehouseCode0;

            txtPrice1.Text = string.Format("{0:0.00}", ModelGoodsCodeCheck.dPrice1);
            txtCode1.Text = ModelGoodsCodeCheck.strCode1;
            txtUnit1.Text = ModelGoodsCodeCheck.strUnit1;
            txtName1.Text = ModelGoodsCodeCheck.strName1;
            txtForm1.Text = ModelGoodsCodeCheck.strForm1;
            txtApproveCode1.Text = ModelGoodsCodeCheck.strRegistrationCode1;
            txtSpec1.Text = ModelGoodsCodeCheck.strSpec1;
            txtMaker1.Text = ModelGoodsCodeCheck.strManufacturer1;
            txtWarehouse1.Text = string.Format("{0}|{1}", ModelGoodsCodeCheck.strWarehouseName1, ModelGoodsCodeCheck.strWarehouseCode1);
            
        }
        private void _save()
        {
            // 取数据
            ModelGoodsCodeCheck.strUnitMinForm0 = txtUnitMinForm0.Text.Trim();            
            if (ModelGoodsCodeCheck.strUnitMinForm0.Equals("")) {
                CommFun._messageBoxShow("入库单位不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }            
            ModelGoodsCodeCheck.dWarehouseRatio0 = (double)nudWarehouseRatio0.Value;
            // 同单位时不能为比例1
            if (ModelGoodsCodeCheck.strUnitMinForm0.Equals(ModelGoodsCodeCheck.strUnit1))
            {
                if (ModelGoodsCodeCheck.dWarehouseRatio0 != 1)
                {
                    CommFun._messageBoxShow("包装单位与入库单位相同,但比例不是1！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (CommFun._messageBoxShow("是否继续保存?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    {
                        return;
                    }
                }
            }
            ModelGoodsCodeCheck.strWarehouseName0 = cbbWarehouse.Text.Split('|')[0];
            ModelGoodsCodeCheck.strWarehouseCode0 = (cbbWarehouse.Text.Split('|').Length == 1) ? "" : cbbWarehouse.Text.Split('|')[1];

            ModelGoodsCodeCheck._saveInfo();
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region 2.系统事件方法
        private void frmConfig_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void cbbWarehouse_DrawItem(object sender, DrawItemEventArgs e)
        {
            CommFun._cbbDrawItem(sender,e);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            _save();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion        
    }
}
